<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: new_attributes_change.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined('_VALID_XTC') or die('Direct Access to this location is not allowed.');

require_once(DIR_FS_INC.'inc.get_tax_rate.php');
require_once(DIR_FS_INC.'inc.get_tax_class_id.php');

$delete = $db->db_query("SELECT
							products_attributes_id
						FROM
							".TABLE_PRODUCTS_ATTRIBUTES."
						WHERE
							products_id = '".$_POST['current_product_id']."'");
while(!$delete->EOF) {
	$delete_file = $db->db_query("SELECT
										products_attributes_filename
									FROM
										".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD."
									WHERE
										products_attributes_id = '".$delete_res['prducts_attributes_id']."'");

	$db->db_query("DELETE FROM ".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD." WHERE products_attributes_id = '".$delete_file->fields['products_attributes_id']."'");
	$delete->MoveNext();
}
$db->db_query("DELETE FROM ".TABLE_PRODUCTS_ATTRIBUTES." WHERE products_id = '".$_POST['current_product_id']."'" );

for ($i = 0; $i < sizeof($_POST['optionValues']); $i++) {
	$line = $db->db_query("SELECT * FROM ".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS." WHERE products_options_values_id = '".$_POST['optionValues'][$i]."'");

	while(!$line->EOF) {
		$optionsID = $line->fields['products_options_id'];
		$line->MoveNext();
	}

	$cv_id = $_POST['optionValues'][$i];
	$value_price = str_replace(',', '.', $_POST[$cv_id.'_price']);

	if (PRICE_IS_BRUTTO == 'true')
		$value_price = ($value_price/((get_tax_rate(get_tax_class_id($_POST['current_product_id'])))+100)*100);

	$value_price = round($value_price,PRICE_PRECISION);

	$value_prefix = $_POST[$cv_id.'_prefix'];
	$value_sortorder = $_POST[$cv_id.'_sortorder'];
	$value_weight_prefix = $_POST[$cv_id.'_weight_prefix'];
	$value_model =  $_POST[$cv_id.'_model'];
	$value_stock =  $_POST[$cv_id.'_stock'];
	$value_weight =  $_POST[$cv_id.'_weight'];
	$value_stock_status =  (isset($_POST[$cv_id.'_stock_status']) ? '1' : '0');
	$value_weight_status =  (isset($_POST[$cv_id.'_weigth_status']) ? '1' : '0');

	$db->db_query("INSERT INTO
						".TABLE_PRODUCTS_ATTRIBUTES."
							(products_id,
							options_id,
							options_view_type,
							options_values_id,
							options_values_price,
							price_prefix,
							attributes_model,
							attributes_stock,
							attributes_stock_status,
							options_values_weight,
							weight_prefix,
							options_values_weight_status,
							sortorder)
					VALUES
							('".$_POST['current_product_id']."',
							'".$optionsID."',
							'".$_POST['options_view_type_'.$optionsID]."',
							'".$_POST['optionValues'][$i]."',
							'".$value_price."',
							'".$value_prefix."',
							'".$value_model."',
							'".$value_stock."',
							'".$value_stock_status."',
							'".$value_weight."',
							'".$value_weight_prefix."',
							'".$value_weight_status."',
							'".$value_sortorder."')");

	$products_attributes_id = $db->db_insert_id();
	
	if ($upload_file = try_upload($cv_id.'_upload_file', DIR_FS_DOWNLOAD, '777', ''))
		$download_file = $upload_file->filename;
	else
		$download_file = $_POST[$cv_id.'_download_file'];
	
	if(!empty($download_file)) {		
		$value_download_expire = $_POST[$cv_id.'_download_expire'];
		$value_download_count = $_POST[$cv_id.'_download_count'];

		$db->db_query("INSERT INTO
							".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD."
								(products_attributes_id,
								products_attributes_filename,
								products_attributes_maxdays,
								products_attributes_maxcount)
						VALUES
								('".$products_attributes_id."',
								'".$download_file."',
								'".$value_download_expire."',
								'".$value_download_count."')");
	}
}